﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GsnHelper;
using ZhongheCms.Models;
namespace ZhongheCms.Controllers
{
    public class JobCustomerController : Controller
    {
        public ActionResult Index(int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswTblJobCustomer.GetCustomer(keywords, GswTblUser.CurrentUser.CustomerId);
            return View(qry);
        }
        public ActionResult Create(int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var customer = id == 0
                ? new GswTblJobCustomer() : GswTblJobCustomer.SingleOrDefault(id);
            customer.BaseInfos =
                GswTblBaseInfo.Fetch("where CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                     " and (FunctionId=121 or FunctionId=123)");
            return View(customer);
        }
        [HttpPost]
        public ActionResult Create(FormCollection c, int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
           
            if (GswTblJobCustomer.SingleOrDefault("where ParentId=" + GswTblUser.CurrentUser.CustomerId + " and  CusShortName='" + c["CusShortName"] + "' and Id<>" + id + "") != null)
                ModelState.AddModelError("", "会员简称" + c["CusShortName"].Trim() + "已经存在");
            if (!string.IsNullOrEmpty(c["LoginName"]) && id == 0)
            {
                var userNameExit = GswTblMemberBase.FirstOrDefault("where MemberUserName='" + c["LoginName"] + "'");
                if (userNameExit != null)
                {
                    ModelState.AddModelError("", "用户名已经存在");
                }
            }
            var customer = id == 0 ? new GswTblJobCustomer() : GswTblJobCustomer.SingleOrDefault(id);
            customer.CusShortName = c["CusShortName"];
            customer.CustomerName = c["CustomerName"];
            customer.Contact = c["Contact"];
            customer.Tel = c["Tel"];
            customer.Fax = c["Fax"];
            customer.Email = c["Email"];
            customer.Address = c["Address"];
            customer.BankName = c["BankName"];
            customer.BankNo = c["BankNo"];
            customer.TaxNo = c["TaxNo"];
            customer.CustomerNo = c["CustomerNo"];
            customer.CusDescrption = c["CusDescrption"];
            customer.WebUrl = c["WebUrl"];
            customer.ParentId = GswTblUser.CurrentUser.CustomerId;
            customer.BusinessType = c["BusinessType"];
            customer.CreateDate = DateTime.Now;
            customer.LoginName = c["LoginName"];
            customer.LoginPwd = c["LoginPwd"];
            customer.CompanySize = c["CompanySize"];
            customer.CustomerLogo = c["CustomerLogo"];
            customer.CusDescrption = c["CusDescrption"];
            

            try
            {
                if (ModelState.IsValid)
                {

                    if (id == 0)
                    {
                        customer.CreateDate = DateTime.Now;
                        customer.SourceFlag = 1;
                        customer.Insert();
                        if (!string.IsNullOrEmpty(customer.LoginName) && !string.IsNullOrEmpty(customer.LoginPwd))
                        {
                            string pwdsalt = EncryptPwd.GenerateSalt();
                            var memberBase = new GswTblMemberBase
                            {
                                AuditFlag = 1,
                                MemberCate = "企业",
                                MemberUserName = customer.LoginName,
                                ParentId = customer.ParentId,
                                RegDate = DateTime.Today,
                                PwdSalt = pwdsalt,
                                PwdHash = EncryptPwd.EncodePassword(customer.LoginPwd, pwdsalt)

                            };
                            memberBase.Insert();
                            GswTblJobCustomer.Update("set BaseId=" + memberBase.Id + " where Id=" + customer.Id);

                        }
                        customer = new GswTblJobCustomer();


                    }
                    else
                    {
                        customer.Update();
                    }
                    ViewBag.scode = "1";
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";
            customer.BaseInfos =
               GswTblBaseInfo.Fetch("where CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                    " and (FunctionId=121 or FunctionId=123)");
            return View(customer);

        }

        public ActionResult Delete(int id, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblJobCustomer.Update("set DelFlag=1 where id=" + id);
            var qry = GswTblJobCustomer.GetCustomer(keywords, GswTblUser.CurrentUser.CustomerId);
            return View("Index", qry);
        }

    }
}
